package com.bindapp.model;

import android.content.Context;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;

import com.bindapp.R;
import com.bindapp.databinding.ActivityCdBinding;
import com.bindapp.util.Static;
import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.OnColorSelectedListener;
import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;

/**
 * Created by apple on 2018/7/11.
 */

public class ControllerCdModel extends BaseModel {

    private ActivityCdBinding binding;

    public ControllerCdModel(final Context context){
        super(context);
        binding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.activity_cd,null,false);
        binding.switch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    binding.redbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_red));
                    binding.yellowbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_yellow));
                    binding.bluebtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_blue));
                    binding.seletcolor.setBackgroundColor(Color.TRANSPARENT);
                }
                binding.statustxt.setText(b?"自动":"渐变");
                binding.statuebg.setBackgroundColor(b? ContextCompat.getColor(context,R.color.linecolor):ContextCompat.getColor(context,R.color.white));
            }
        });
        binding.setClick(new ViewClick());
    }

    public class ViewClick{
        public void onClick(View view){
            if(!binding.switch2.isChecked()){
                switch(view.getId()){
                    case R.id.otherbtn:
                        ColorPickerDialogBuilder
                                .with(context)
                                .setTitle("选择颜色")
                                .initialColor(Color.WHITE)
                                .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
                                .density(12)
                                .setOnColorSelectedListener(new OnColorSelectedListener() {
                                    @Override
                                    public void onColorSelected(int selectedColor) {
                                        Static.myLog(""+Integer.toHexString(selectedColor));
                                        //toast("onColorSelected: 0x" + Integer.toHexString(selectedColor));
                                    }
                                })
                                .setPositiveButton("确定", new ColorPickerClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
                                        //changeBackgroundColor(selectedColor);
                                        //Static.myLog(""+Integer.toHexString(selectedColor));
                                        binding.seletcolor.setBackgroundColor(selectedColor);
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                    }
                                })
                                .build()
                                .show();
                        break;
                    case R.id.redbtn:
                        binding.redbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.round_red));
                        binding.yellowbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_yellow));
                        binding.bluebtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_blue));
                        break;
                    case R.id.yellowbtn:
                        binding.redbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_red));
                        binding.yellowbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.round_light_yellow));
                        binding.bluebtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_blue));
                        break;
                    case R.id.bluebtn:
                        binding.redbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_red));
                        binding.yellowbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_yellow));
                        binding.bluebtn.setBackground(ContextCompat.getDrawable(context,R.drawable.round_blue));
                        break;
                    case R.id.redtxt:
                        binding.redbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.round_red));
                        binding.yellowbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_yellow));
                        binding.bluebtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_blue));
                        break;
                    case R.id.yellowtxt:
                        binding.redbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_red));
                        binding.yellowbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.round_light_yellow));
                        binding.bluebtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_blue));
                        break;
                    case R.id.bluetxt:
                        binding.redbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_red));
                        binding.yellowbtn.setBackground(ContextCompat.getDrawable(context,R.drawable.circle_yellow));
                        binding.bluebtn.setBackground(ContextCompat.getDrawable(context,R.drawable.round_blue));
                        break;
                }
            }


        }
    }


    @Override
    public ActivityCdBinding getBinding() {
        return binding;
    }
}
